# -*- coding:utf-8 -*-
import FreeSimpleGUI as sg                        # Part 1 - The import
from csv_spliter import split_csv

import ctypes
import platform

def make_dpi_aware():
    if int(platform.release()) >= 8:
        ctypes.windll.shcore.SetProcessDpiAwareness(True)
make_dpi_aware()

# Define the window's contents
percentage_frame = [
    [
        sg.Text("拆分比例设置")
    ],
    [
        sg.Text("训练数据比例："), sg.InputText(key="_TRAIN_", default_text="6", readonly=False, enable_events=True, size=(10, 1)),
        sg.Text("验证数据比例："), sg.InputText(key="_VAL_", default_text="2", readonly=False, enable_events=True, size=(10, 1)),
        sg.Text("测试数据比例："), sg.InputText(key="_TEST_", default_text="2", readonly=False, enable_events=True, size=(10, 1))
    ],
]

out_frame = [
    [
        sg.Checkbox("输出到源文件夹", key='_OUT_TO_SRCFOLDER', default=True, enable_events=True)
    ],
    [
        sg.Text("train完整路径或文件名:", size=(20, 1)),
        sg.InputText(key="_TRAIN_OUT_", default_text="train.csv", readonly=False, enable_events=True, size=(70, 1)),
    ],
    [
        sg.Text("val完整路径或文件名:", size=(20, 1)),
        sg.InputText(key="_VAL_OUT_", default_text="val.csv", readonly=False, enable_events=True, size=(70, 1)),
    ],
    [
        sg.Text("test完整路径或文件名:", size=(20, 1)),
        sg.InputText(key="_TEST_OUT_", default_text="test.csv", readonly=False, enable_events=True, size=(70, 1)),
    ]
]

layout = [
    [sg.Text("选择csv文件:", size=(15, 1)),
     sg.InputText(key="_CSV_FILE_", readonly=False, enable_events=True, size=(60, 1)),
     sg.FilesBrowse(key="_FILE_BROWSE_", file_types = (('CSV files', '*.csv'),), button_text="浏览", target="_CSV_FILE_", size=(14,1))
     ],
    [sg.Text("源csv文件编码:", size=(15, 1)),
     sg.InputText(key="_CSV_ENCODING_", default_text="utf-8", readonly=False, enable_events=True, size=(20, 1))
     ],
    [sg.Frame('', percentage_frame, background_color="lavender",  pad=(0,0), expand_x=True, border_width=0, grab=True)],
    [sg.Frame('', out_frame, background_color="Gainsboro", pad=(0, 0), expand_x=True, border_width=0, grab=True)],
    [sg.Button('确定', key='_OK_')],
    [sg.Text("当前状态：", key='_FILE_INFO_', text_color='black', size=(50, 1))]
]

# Create the window
window = sg.Window('CSV文件拆分器', layout, size=(800,350))      # Part 3 - Window Defintion

def main():
    train_pro, val_pro, test_pro = 6, 2, 2
    out_to_src_folder = True
    train_out = 'train.csv'
    val_out = 'val.csv'
    test_out = 'test.csv'

    while True:
        event, values = window.read()

        if event == sg.WINDOW_CLOSED:
            break
        if event == '_CSV_FILE_':
            window['_FILE_INFO_'].update('当前状态：' + values['_CSV_FILE_'], text_color='red')
        if event == '_OUT_TO_SRCFOLDER':
            print(values['_OUT_TO_SRCFOLDER'])
        if event == '_OK_':
            csv_file_path=values['_CSV_FILE_']
            train_pro, val_pro, test_pro = int(values['_TRAIN_']), int(values['_VAL_']), int(values['_TEST_'])
            out_to_src_folder = values['_OUT_TO_SRCFOLDER']
            train_out = values['_TRAIN_OUT_']
            val_out = values['_VAL_OUT_']
            test_out = values['_TEST_OUT_']
            encoding = values['_CSV_ENCODING_']
            split_csv(csv_file_path,
                      train_pro, val_pro, test_pro,
                      encoding,
                      out_to_src_folder,
                      train_out, val_out, test_out)
            window['_FILE_INFO_'].update('当前状态：拆分csv完成！！', text_color='yellow')

if __name__ == '__main__':
    main()
